layui.use(['table', 'admin', 'ax','treetable'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var treetable = layui.treetable;
    var admin = layui.admin;

    /**
     * 入库表管理
     */
    var PutInWarehouse = {
        tableId: "putInWarehouseTable"
    };

    /**
     * 初始化表格的列
     */
    PutInWarehouse.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'productName', sort: false, title: '产品大类'},
            {field: 'productModelName', sort: false, title: '产品型号'},
            {field: 'productOrderName', sort: false, title: '产品品号'},
            {field: 'batchNumber', sort: true, title: '批次'},
            {field: 'warhouseName', sort: false, title: '仓库'},
            {field: 'amount', sort: false, title: '数量'},
            {field: 'operator', sort: false, title: '操作人'},
            {field: 'operatorDept', sort: false, title: '操作人部门'},
            {field: 'createTime', sort: true, title: '入库时间'},
            {
                field: 'codeType', sort: true, title: '入库状态', templet: function (d) {

                        if (d.code === 1) {
                            return '暂无入库';
                        } else if (d.code === 2){
                            return '已经入库';
                        }else
                            return '';
                        }
            },
            {align: 'center', width: 200, fixed: 'right', toolbar: '#tableBar', title: '操作'}
        ]];
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PutInWarehouse.tableId,
        url: Feng.ctxPath + '/putInWarehouse/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: PutInWarehouse.initColumn()
    });

    /**
     * 点击查询按钮
     */
    PutInWarehouse.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(PutInWarehouse.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    PutInWarehouse.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/putInWarehouse/add';
    };

    /**
     * 导出excel按钮
     */
    PutInWarehouse.exportExcel = function () {
        var checkRows = table.checkStatus(PutInWarehouse.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    PutInWarehouse.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/putInWarehouse/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    PutInWarehouse.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/putInWarehouse/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(PutInWarehouse.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    PutInWarehouse.onPutInWarhouse = function (data) {

        var operation = function () {

            var ajax = new $ax(Feng.ctxPath + "/putInWarehouse/putInWarhouse",function (data) {
                if (data.code === 200){
                    Feng.success("入库成功!");
                    console.log($("#tableBar #demo").innerHTML);
                    $("#tableBar #demo").html("已入库");
                    table.reload(PutInWarehouse.tableId);
                }else {
                    Feng.error("入库失败!" + data.message + "!");
                }
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否入库?", operation);
    };
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PutInWarehouse.tableId,
        url: Feng.ctxPath + '/putInWarehouse/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: PutInWarehouse.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PutInWarehouse.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        PutInWarehouse.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        PutInWarehouse.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + PutInWarehouse.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            PutInWarehouse.openEditDlg(data);
        } else if (layEvent === 'delete') {
            PutInWarehouse.onDeleteItem(data);
        } else  if (layEvent === 'inWarhouse') {
            PutInWarehouse.onPutInWarhouse(data);
        }
    });
});
